﻿using MediatR;
using SmartCity.ProcessEngine.API.Read.Application.Commands.FormNode;
using SmartCity.ProcessEngine.API.Read.DTO;
using SmartCity.ProcessEngine.Domain;
using SmartCity.ProcessEngine.Errorcode;
using SmartCity.ProcessEngine.Infrastructure.Interfaces;

namespace SmartCity.ProcessEngine.API.Read.Application.CommandHandlers.FormNode
{
    /// <summary>
    /// 节点查询命令处理
    /// </summary>
    public class NodeQueryCommandHandler:IRequestHandler<NodeQueryCommand,ApiResult<List<FormNodeDto>>>
    {
        private readonly IBaseRepository<WF_Form> formRep;
        private readonly IBaseRepository<WF_FromType> typeRep;
        private readonly IBaseRepository<Industry> indestryRep;
        private readonly IBaseRepository<WF_Node> nodeRep;

        public NodeQueryCommandHandler(IBaseRepository<WF_Form> formRep,IBaseRepository<WF_FromType> typeRep,IBaseRepository<Industry> indestryRep,IBaseRepository<WF_Node> nodeRep)
        {
            this.formRep = formRep;
            this.typeRep = typeRep;
            this.indestryRep = indestryRep;
            this.nodeRep = nodeRep;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回节点列表</returns>
        public Task<ApiResult<List<FormNodeDto>>> Handle(NodeQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<FormNodeDto>> result = new ApiResult<List<FormNodeDto>>();
            result.code = ApiEnum.SearchSuccess;
            result.message = "成功";

            var list = from a in nodeRep.GetAll()
                       join b in formRep.GetAll() on a.FormNo equals b.FormNo
                       join c in typeRep.GetAll() on b.TypeNo equals c.TypeNo
                       join d in indestryRep.GetAll() on c.IndustryId equals d.IndustryId
                       where !a.IsDeleted
                       select new FormNodeDto
                       {
                           NodeId = a.NodeId,
                           NodeNo = a.NodeNo,
                           NodeName = a.NodeName,
                           FormNo = b.FormNo,
                           FormName = b.FormName,
                           TypeNo = c.TypeNo,
                           TypeName = c.TypeName,
                           IndustryId = d.IndustryId,
                           IndustryName = d.IndustryName,
                       };
            if(request.IndustryId > 0)
            {
                list = list.Where(x => x.IndustryId == request.IndustryId);
            }
            if (!string.IsNullOrEmpty(request.FormNo))
            {
                list = list.Where(x=>x.FormNo == request.FormNo);
            }
            if (!string.IsNullOrEmpty(request.TypeNo))
            {
                list = list.Where(x => x.TypeNo == request.TypeNo);
            }
            result.data = list.ToList();
            return Task.FromResult(result);
        } 
    }
}
